//////////////////////////////////////////ok
#include"stdafx.h"
#include "bochs.h"



void IA32_CPU::XOR_EbGb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
    result = op1 ^ op2;
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
    result = op1 ^ op2;
    write_RMW_virtual_byte(result);
  }
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
}

void IA32_CPU::XOR_GbEb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, result;

  op1 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op2 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_virtual_byte(i->seg(), IA32_RMAddr(i), &op2);
  }

  result = op1 ^ op2;
  IA32_WRITE_8BIT_REGx(i->nnn(), i->extend8bitL(), result);
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
}

void IA32_CPU::XOR_ALIb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, sum;
  op1 = AL;
  op2 = i->Ib();
  sum = op1 ^ op2;
  AL = sum;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(sum, IA32_INSTR_LOGIC8);
}

void IA32_CPU::XOR_EbIb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = i->Ib();
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
    result = op1 ^ op2;
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
    result = op1 ^ op2;
    write_RMW_virtual_byte(result);
  }
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
}

void IA32_CPU::OR_EbIb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = i->Ib();
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
    result = op1 | op2;
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
    result = op1 | op2;
    write_RMW_virtual_byte(result);
  }
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
}

void IA32_CPU::NOT_Eb(Ia32_Instruction_c *i)
{
  Bit8u op1_8, result_8;

  if (i->modC0()) 
  {
    op1_8 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
    result_8 = ~op1_8;
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result_8);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1_8);
    result_8 = ~op1_8;
    write_RMW_virtual_byte(result_8);
  }

}

void IA32_CPU::OR_EbGb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
    result = op1 | op2;
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
    result = op1 | op2;
    write_RMW_virtual_byte(result);
  }
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
}

void IA32_CPU::OR_GbEb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, result;

  op1 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op2 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_virtual_byte(i->seg(), IA32_RMAddr(i), &op2);
  }

#if defined(IA32_HostAsm_Or8)
  Bit32u flags32;
  asmOr8(result, op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  result = op1 | op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
  IA32_WRITE_8BIT_REGx(i->nnn(), i->extend8bitL(), result);
}

void IA32_CPU::OR_ALIb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, result;

  op1 = AL;
  op2 = i->Ib();
#if defined(IA32_HostAsm_Or8)
  Bit32u flags32;
  asmOr8(result, op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  result = op1 | op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
  AL = result;
}

void IA32_CPU::AND_EbGb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
#if defined(IA32_HostAsm_And8)
    Bit32u flags32;
    asmAnd8(result, op1, op2, flags32);
    IA32_setEFlagsOSZAPC(flags32);
#else
    result = op1 & op2;
#endif
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
#if defined(IA32_HostAsm_And8)
    Bit32u flags32;
    asmAnd8(result, op1, op2, flags32);
    IA32_setEFlagsOSZAPC(flags32);
#else
    result = op1 & op2;
#endif
    write_RMW_virtual_byte(result);
  }
#if !defined(IA32_HostAsm_And8)
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
}

void IA32_CPU::AND_GbEb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, result;

  op1 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op2 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_virtual_byte(i->seg(), IA32_RMAddr(i), &op2);
  }
#if defined(IA32_HostAsm_And8)
  Bit32u flags32;
  asmAnd8(result, op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  result = op1 & op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
  IA32_WRITE_8BIT_REGx(i->nnn(), i->extend8bitL(), result);
}

void IA32_CPU::AND_ALIb(Ia32_Instruction_c *i)
{
  Bit8u op1, op2, result;

  op1 = AL;
  op2 = i->Ib();
#if defined(IA32_HostAsm_And8)
  Bit32u flags32;
  asmAnd8(result, op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  result = op1 & op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
  AL = result;
}

void IA32_CPU::AND_EbIb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1, result;

  op2 = i->Ib();
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
#if defined(IA32_HostAsm_And8)
    Bit32u flags32;
    asmAnd8(result, op1, op2, flags32);
    IA32_setEFlagsOSZAPC(flags32);
#else
    result = op1 & op2;
#endif
    IA32_WRITE_8BIT_REGx(i->rm(), i->extend8bitL(), result);
  }
  else 
  {
    read_RMW_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
#if defined(IA32_HostAsm_And8)
    Bit32u flags32;
    asmAnd8(result, op1, op2, flags32);
    IA32_setEFlagsOSZAPC(flags32);
#else
    result = op1 & op2;
#endif
    write_RMW_virtual_byte(result);
  }
#if !defined(IA32_HostAsm_And8)
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
}

void IA32_CPU::TEST_EbGb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1;

  op2 = IA32_READ_8BIT_REGx(i->nnn(),i->extend8bitL());
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
  }
#if defined(IA32_HostAsm_Test8)
  Bit32u flags32;
  asmTest8(op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  Bit8u result = op1 & op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
}

void IA32_CPU::TEST_ALIb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1;

  op1 = AL;
  op2 = i->Ib();
#if defined(IA32_HostAsm_Test8)
  Bit32u flags32;
  asmTest8(op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  Bit8u result = op1 & op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
}

void IA32_CPU::TEST_EbIb(Ia32_Instruction_c *i)
{
  Bit8u op2, op1;

  op2 = i->Ib();
  if (i->modC0()) 
  {
    op1 = IA32_READ_8BIT_REGx(i->rm(),i->extend8bitL());
  }
  else 
  {
    read_virtual_byte(i->seg(), IA32_RMAddr(i), &op1);
  }
#if defined(IA32_HostAsm_Test8)
  Bit32u flags32;
  asmTest8(op1, op2, flags32);
  IA32_setEFlagsOSZAPC(flags32);
#else
  Bit8u result;
  result = op1 & op2;
  IA32_SET_FLAGS_OSZAPC_RESULT_8(result, IA32_INSTR_LOGIC8);
#endif
}
